# ---------- ---------- ---------- ---------- ---------- ----------
#
#   This makefile generates the peers website.
#
# ---------- ---------- ---------- ---------- ---------- ----------


STAGE_DIR=content
BUILD_DIR=output
SITEMAP=src/sitemap/index.md


target: build


.PHONY: help
help:
	@echo "make           Build website"
	@echo "make server    Run a local test server"
	@echo "make clean     Delete build files and output"


.PHONY: sitemap
sitemap:
	@cp etc/templates/sitemap.md $(SITEMAP)
	@echo "" >> $(SITEMAP)
	@echo "## Directories" >> $(SITEMAP)
	@echo "" >> $(SITEMAP)
	@cd src && \
            find . -type d \
                 -not -path "." \
                | sort \
                | sed 's/\.\//\.\.\//g' \
                | awk '/$$/{ print "- ["$$0"]("$$0")" }' \
            >> ../$(SITEMAP) && \
            cd ..
	@echo "" >> $(SITEMAP)
	@echo "## Pages" >> $(SITEMAP)
	@echo "" >> $(SITEMAP)
	@cd src && \
            find . -type f \
                 -not -path "./media/*" \
                 -not -name ".htaccess" \
                 -not -name "*.css" \
                | sort \
                | sed 's/\.\//\.\.\//g' \
                | awk '/$$/{ print "- ["$$0"]("$$0")" }' \
            >> ../$(SITEMAP) && \
            cd ..


.PHONY: build
build: sitemap
	@rsync -avh --delete src/ $(STAGE_DIR)/
	@cp $(STAGE_DIR)/media/hack.png $(STAGE_DIR)/favicon.ico
	@cp $(STAGE_DIR)/media/PeersCo_websitebanner_v3.png $(STAGE_DIR)/banner.png
	@cp $(STAGE_DIR)/media/glider-circles.svg $(STAGE_DIR)/logo.svg
	@fenrisg \
            markdown-smart \
            $(STAGE_DIR)/ \
            etc/templates/peers.html \
            etc/filters/links-md-to-html.lua \
            light \
            dark/ \
            $(BUILD_DIR)
	@fenrisg \
            markdown-smart \
            $(STAGE_DIR)/ \
            etc/templates/peers.html \
            etc/filters/links-md-to-html.lua \
            dark \
            ../ \
            $(BUILD_DIR)/dark


.PHONY: server
server:
	@cd $(BUILD_DIR) && python3 -m http.server


.PHONY: clean
clean:
	@rm --recursive --force $(BUILD_DIR) $(STAGE_DIR)


.PHONY: rebuild
rebuild: clean build
